//
//  QBUserInfoCell.m
//  QiuBai
//
//  Created by yaohongchao on 5/1/16.
//  Copyright © 2016 yaohongchao. All rights reserved.
//

#import "QBUserInfoView.h"
#import "UIImage+RoundedCorner.h"
#import "SDWebImageDownloader.h"
#import "SDImageCache.h"

@interface QBUserInfoView()
@property(nonatomic,weak)UIView *topView;
@property(nonatomic,weak)UIImageView *imgVHead;
@property(nonatomic,weak)UILabel *lblName;
@property(nonatomic,weak)UIButton *btnHot;
@property(nonatomic,weak)UIView *hotView;
@end


@implementation QBUserInfoView

-(instancetype)init{
    self = [super init];
    if(self){
        [self setui];
        [self makeConstraint];
    }
    return self;
}

-(void)setui{
    
    UIImageView *imgVHead = [[UIImageView alloc]init];
    [self addSubview:imgVHead];
    self.imgVHead = imgVHead;
    imgVHead.image = [UIImage imageNamed:@"default_avatar"];
//    [imgVHead jm_setCornerRadius:35/2 withBorderColor:nil borderWidth:0];
    UIView *hotView = [[UIView alloc]init];
    [self addSubview:hotView];
    self.hotView = hotView;
 
    
    UIButton *btnHot = [[UIButton alloc]init];
    btnHot.userInteractionEnabled = NO;
    [btnHot setImage:[UIImage imageNamed:@"qb_hot"] forState:UIControlStateNormal];
    [btnHot setTitle:@"  热门" forState:UIControlStateNormal];
    btnHot.titleLabel.font = [UIFont systemFontOfSize:11];
    [btnHot setTitleColor:[UIColor colorWithHexString:@"#9c9ca1"] forState:UIControlStateNormal];
    [hotView addSubview:btnHot];
    self.btnHot = btnHot;
    
    UILabel *lblName =[[UILabel alloc]init];
    lblName.textColor = [UIColor colorWithHexString:@"#9c9ca1"];
    lblName.font = [UIFont systemFontOfSize:15];
    [self addSubview:lblName];
    self.lblName = lblName;
   
}

-(void)makeConstraint{
    WS(weakSelf);

    [self.imgVHead mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(weakSelf).offset(kMargin);
        make.height.width.mas_equalTo(35);
    }];
    [self.hotView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf).offset(-kMargin);
        make.centerY.equalTo(weakSelf.imgVHead.mas_centerY);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(50);
    }];
    [self.btnHot mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.hotView);
    }];
    [self.lblName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.imgVHead.mas_right).offset(kMargin);
        make.right.equalTo(weakSelf.hotView.mas_left).offset(-kMargin);
        make.centerY.equalTo(weakSelf.imgVHead.mas_centerY);
        make.height.mas_equalTo(25);
    }];

}

-(void)setItem:(Item*)item{
    _item = item;
    WS(weakSelf);
    if([[SDImageCache sharedImageCache] diskImageExistsWithKey:item.user.headIconUrl]){
        self.imgVHead.image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:item.user.headIconUrl];
    }else{
        [[SDWebImageDownloader sharedDownloader]downloadImageWithURL:[NSURL URLWithString:item.user.headIconUrl] options:SDWebImageDownloaderHighPriority progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
            if(finished && image){
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    CGFloat width = image.size.width / 2;
                    UIImage *img = [UIImage jm_imageWithRoundedCornersAndSize:image.size JMRadius:JMRadiusMake(width, width, width, width) borderColor:nil borderWidth:0 backgroundColor:nil backgroundImage:image withContentMode:UIViewContentModeScaleAspectFit];
                    
                    [[SDImageCache sharedImageCache]storeImage:img forKey:item.user.headIconUrl toDisk:YES];
                    dispatch_sync(dispatch_get_main_queue(), ^{
                        weakSelf.imgVHead.image = img;
                    });
                });
                
            }
        }];
    }
    if([item.type isEqualToString:@"hot"]){
        self.btnHot.hidden = NO;
    }else{
        self.btnHot.hidden = YES;
    }
    self.lblName.text = item.user.login;
}



@end
